package jparchess.engine;

// Definition class D - only java way to have access from everywhere to sort of global constants
public class D {
	public static final int MAX_PLY = 10;
	
	public static final int EMPTY = 0;
	public static final int BLACK_PAWN = -1;
	public static final int BLACK_QUEEN = -2;
	public static final int BLACK_ROOK = -3;
	public static final int BLACK_BISHOP = -4;
	public static final int BLACK_KNIGHT = -5;
	public static final int BLACK_KING = -6;
	public static final int WHITE_PAWN = 1;
	public static final int WHITE_QUEEN = 2;
	public static final int WHITE_ROOK = 3;
	public static final int WHITE_BISHOP = 4;
	public static final int WHITE_KNIGHT = 5;	
	public static final int WHITE_KING = 6;
    public static final boolean WHITE_TO_MOVE = true;
    public static final boolean BLACK_TO_MOVE = false;
	
	public static final int WHITE_CASTLING_00 = 1;
	public static final int WHITE_CASTLING_000 = 2;
	public static final int BLACK_CASTLING_00 = -1;
	public static final int BLACK_CASTLING_000 = -2;
	
	public static final int PAWN_VALUE = 100;
	public static final int KNIGHT_VALUE = 300;
	public static final int BISHOP_VALUE = 300;
	public static final int ROOK_VALUE = 500;
	public static final int QUEEN_VALUE = 950;
	public static final int KING_VALUE = 9999;
	
	public static final int A8 = 56; public static final int B8 = 57; public static final int C8 = 58; public static final int D8 = 59;
	public static final int E8 = 60; public static final int F8 = 61; public static final int G8 = 62; public static final int H8 = 63;
	public static final int A7 = 48; public static final int B7 = 49; public static final int C7 = 50; public static final int D7 = 51;
	public static final int E7 = 52; public static final int F7 = 53; public static final int G7 = 54; public static final int H7 = 55;
	public static final int A6 = 40; public static final int B6 = 41; public static final int C6 = 42; public static final int D6 = 43;
	public static final int E6 = 44; public static final int F6 = 45; public static final int G6 = 46; public static final int H6 = 47;
	public static final int A5 = 32; public static final int B5 = 33; public static final int C5 = 34; public static final int D5 = 35;
	public static final int E5 = 36; public static final int F5 = 37; public static final int G5 = 38; public static final int H5 = 39;
	public static final int A4 = 24; public static final int B4 = 25; public static final int C4 = 26; public static final int D4 = 27;
	public static final int E4 = 28; public static final int F4 = 29; public static final int G4 = 30; public static final int H4 = 31;
	public static final int A3 = 16; public static final int B3 = 17; public static final int C3 = 18; public static final int D3 = 19;
	public static final int E3 = 20; public static final int F3 = 21; public static final int G3 = 22; public static final int H3 = 23;
	public static final int A2 =  8; public static final int B2 =  9; public static final int C2 = 10; public static final int D2 = 11;
	public static final int E2 = 12; public static final int F2 = 13; public static final int G2 = 14; public static final int H2 = 15;
	public static final int A1 =  0; public static final int B1 =  1; public static final int C1 =  2; public static final int D1 =  3;
	public static final int E1 =  4; public static final int F1 =  5; public static final int G1 =  6; public static final int H1 =  7;
	
	public static final String[] squareName = new String[] {
		"A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1",
		"A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2",
		"A3", "B3", "C3", "D3", "E3", "F3", "G3", "H3",
		"A4", "B4", "C4", "D4", "E4", "F4", "G4", "H4",
		"A5", "B5", "C5", "D5", "E5", "F5", "G5", "H5",
		"A6", "B6", "C6", "D6", "E6", "F6", "G6", "H6",
		"A7", "B7", "C7", "D7", "E7", "F7", "G7", "H7",
		"A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8",
	};
	
	public static final String[] portableSquareName = new String[] {
		"a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
		"a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
		"a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
		"a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
		"a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
		"a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
		"a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
		"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
	};

	public static final String[] pieceName = new String[] {
		"BLACK_KING", "BLACK_KNIGHT", "BLACK_BISHOP", "BLACK_ROOK", "BLACK_QUEEN", "BLACK_PAWN", 
		"EMPTY",
		"WHITE_PAWN", "WHITE_QUEEN", "WHITE_ROOK", "WHITE_BISHOP", "WHITE_KNIGHT", "WHITE_KING"
	};
	
	public static final String[] portableName = new String[] {
		"K", "N", "B", "R", "Q", "", 
		"0",
		"", "Q", "R", "B", "N", "K"
	};

	
	// *** EVALUATION
	
	public static int[] PAWN_TABLE = new int[]
	{
	  0,  0,  0,  0,  0,  0,  0,  0,
	 50, 50, 50, 50, 50, 50, 50, 50,
	 10, 10, 20, 30, 30, 20, 10, 10,
	  5,  5, 10, 27, 27, 10,  5,  5,
	  0,  0,  0, 25, 25,  0,  0,  0,
	  5, -5,-10,  0,  0,-10, -5,  5,
	  5, 10, 10,-25,-25, 10, 10,  5,
	  0,  0,  0,  0,  0,  0,  0,  0
	};
	public static int[] KNIGHT_TABLE = new int[]
	{
	 -50,-40,-30,-30,-30,-30,-40,-50,
	 -40,-20,  0,  0,  0,  0,-20,-40,
	 -30,  0, 10, 15, 15, 10,  0,-30,
	 -30,  5, 15, 20, 20, 15,  5,-30,
	 -30,  0, 15, 20, 20, 15,  0,-30,
	 -30,  5, 10, 15, 15, 10,  5,-30,
	 -40,-20,  0,  5,  5,  0,-20,-40,
	 -50,-40,-20,-30,-30,-20,-40,-50,
	};

	public static int[] BISHOP_TABLE = new int[]
	{
	 -20,-10,-10,-10,-10,-10,-10,-20,
	 -10,  0,  0,  0,  0,  0,  0,-10,
	 -10,  0,  5, 10, 10,  5,  0,-10,
	 -10,  5,  5, 10, 10,  5,  5,-10,
	 -10,  0, 10, 10, 10, 10,  0,-10,
	 -10, 10, 10, 10, 10, 10, 10,-10,
	 -10,  5,  0,  0,  0,  0,  5,-10,
	 -20,-10,-40,-10,-10,-40,-10,-20,
	};
	
	public static int[] ROOK_TABLE = new int[]
	{
	 0,  0,  0,  0,  0,  0,  0,  0,
	  5, 10, 10, 10, 10, 10, 10,  5,
	 -5,  0,  0,  0,  0,  0,  0, -5,
	 -5,  0,  0,  0,  0,  0,  0, -5,
	 -5,  0,  0,  0,  0,  0,  0, -5,
	 -5,  0,  0,  5,  5,  0,  0, -5,
	 -5,  5,  5,  5,  5,  5,  5, -5,
	  0,  5,  5,  10,  10,  5,  5,  0
	};
	
	public static int[] QUEEN_TABLE = new int[]
	{
	-20,-10,-10, -5, -5,-10,-10,-20,
	-10,  0,  0,  0,  0,  0,  0,-10,
	-10,  0,  5,  5,  5,  5,  0,-10,
	 -5,  0,  5,  5,  5,  5,  0, -5,
	  0,  0,  5,  5,  5,  5,  0, -5,
	-10,  5,  5,  5,  5,  5,  0,-10,
	-10,  0,  5,  0,  0,  0,  0,-10,
	-20,-10,-10, -5, -5,-10,-10,-20, 
	};

	public static int[] KING_TABLE = new int[]
	{
	  -30, -40, -40, -50, -50, -40, -40, -30,
	  -30, -40, -40, -50, -50, -40, -40, -30,
	  -30, -40, -40, -50, -50, -40, -40, -30,
	  -30, -40, -40, -50, -50, -40, -40, -30,
	  -20, -30, -30, -40, -40, -30, -30, -20,
	  -10, -20, -20, -20, -20, -20, -20, -10, 
	   20,  20,   0,   0,   0,   0,  20,  20,
	   20,  30,  10,   0,   0,  10,  30,  20
	};

	public static int[] KingTableEndGame = new int[]
	{
	 -50,-40,-30,-20,-20,-30,-40,-50,
	 -30,-20,-10,  0,  0,-10,-20,-30,
	 -30,-10, 20, 30, 30, 20,-10,-30,
	 -30,-10, 30, 40, 40, 30,-10,-30,
	 -30,-10, 30, 40, 40, 30,-10,-30,
	 -30,-10, 20, 30, 30, 20,-10,-30,
	 -30,-30,  0,  0,  0,  0,-30,-30,
	 -50,-30,-30,-30,-30,-30,-30,-50
	};
	
	
	// *** NOTATIONS
	
	// http://en.wikipedia.org/wiki/Portable_game_notation
	// TODO missing check sign and promotions - cause no check found yet in engine
	public static String portableGameNotation(Move move) {
		if (move.castling == D.WHITE_CASTLING_00 || move.castling == D.BLACK_CASTLING_00) return "O-O";
		if (move.castling == D.WHITE_CASTLING_000 || move.castling == D.BLACK_CASTLING_000) return "O-O-O";
		
		if (move.captured == 0)
			 return (portableName[move.piece+6] + portableSquareName[move.to]);
		else return (portableName[move.piece+6] + "x" + portableSquareName[move.to]);
	
		// promotions and check!!!
	}
	
	
	// *** LOGGING
	public static void log(Move move) {
		System.out.println("LOG: " + "Move "+ pieceName[move.piece+6] + " from " + squareName[move.from] + " to " + squareName[move.to]);
	}
	
	public static void logDebug(Move move) {
		System.out.println("LOGDEBUG: " + "Move "+ move.piece+6 + " from " + move.from + " to " + move.to);
	}
	public static void logSearch(Move move, int depth, int score) {
		String tabs = null;
		for (int i=0;i<depth;i++) tabs += "\t\t";
		System.out.println(tabs+"LOGS: score::" +score+ "depth::" +depth + pieceName[move.piece+6] + " from " + squareName[move.from] + " to " + squareName[move.to]);
	}
}
